/**
 * @Author:刘忻忻,高志斐
 * @Date:2020/7/1
 * @Description:这是登录/注册页面
 */
import React, { Component } from 'react';
import {Toast, Button, WhiteSpace, InputItem, List, Icon, NavBar} from "antd-mobile";
import axios from "axios"
import { setCookie,setRiskCookie } from "./Component_Login/GetCookie"
import {URL} from "../../index";
export default class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            accountCode: "",//手机号
            captcha: "",//验证码
        }
        this.get_accountCode = this.get_accountCode.bind(this);
        this.get_captcha = this.get_captcha.bind(this);
        this.valid = this.valid.bind(this);
        this.login = this.login.bind(this);
        this.register = this.register.bind(this)
    }
    //获取用户填写的用户名（手机号）
    get_accountCode(accountCode) {
        this.setState({
            accountCode
        });
    }
    //获取用户填写的验证码
    get_captcha(captcha) {
        this.setState({
            captcha
        })
    }
    //发送验证码的逻辑
    valid() {
        //判断用户名是否为空，如果是空的，给出提示
        if (this.state.accountCode === "") {
            Toast.info("请输入手机号")
        } else {
            //判断用户名是否存在
            axios({
                method: "GET",
                url: URL + "/management/user/validateUserAccount",
                params: {
                    phoneNumber: this.state.accountCode,
                }
            }).then(res => {
                //用户存在,发送登录验证码
                if (res.data.code === 201) {
                    axios({
                        method: "GET",
                        url: URL + "/management/user/sendCode",
                        params: {
                            phoneNumber: this.state.accountCode,
                            identification: 1
                        }
                    }).then(res1 => {
                        if (res1.data.code === 200) {
                            Toast.info("验证码发送成功")
                        } else {
                            Toast.fail("验证码发送失败，请检查手机号输入是否正确")
                        }
                    })
                } else {
                    //用户不存在，发送注册验证码
                    axios({
                        method: "GET",
                        url: URL + "/management/user/sendCode",
                        params: {
                            phoneNumber: this.state.accountCode,
                            identification: 2
                        }
                    }).then(res1 => {
                        if (res1.data.code === 200) {
                            Toast.info("验证码发送成功")

                        } else {
                            Toast.fail("验证码发送失败，请检查手机号输入是否正确")
                        }
                    })
                }
            })
        }
    }

    //登录逻辑
    login() {
        if (this.state.accountCode === "") {
            Toast.info("请输入手机号")
        }
        else if(this.state.captcha === ""){
            Toast.info("请输入验证码")
        }else{
            //判断用户名是否存在
            axios({
                method: "GET",
                url: URL + "/management/user/validateUserAccount",
                params: {
                    phoneNumber: this.state.accountCode,
                }
            }).then(res => {
                //用户存在,发送登录请求，后台判断手机号和验证码是否正确
                if (res.data.code === 201) {
                    axios({
                        method: "GET",
                        url: URL + "/management/user/login",
                        params: {
                            phoneNumber: this.state.accountCode,
                            loginCode: this.state.captcha
                        }
                    }).then(res => {
                        //登录后先判断是否进行了风险测评
                        //尚未进行风险测评，则跳转到风险测评页
                        if (res.data.code === 200 && res.data.riskLevel === 0) {
                            setCookie(res.data.nickName, res.data.id,res.data.images)
                            this.props.history.push("/riskEval")
                            //若已进行风险测评，跳转到首页
                        }else if(res.data.code === 200 && res.data.riskLevel !== 0){
                            setCookie(res.data.nickName, res.data.id,res.data.images)
                            setRiskCookie(res.data.riskLevel)
                            this.props.history.push("/terran/home")
                        }
                        else {
                            Toast.fail("登录失败，请检查手机号和验证码是否正确")
                        }
                    })
                    //用户不存在，提示用户先注册再登录
                }else{
                    Toast.info("您还未注册，请先注册")
                }
            })
        }
        }
    //注册逻辑
    register() {
        if (this.state.accountCode === "") {
            Toast.info("请输入手机号")
        }
        else if(this.state.captcha === ""){
            Toast.info("请输入验证码")
        }else {
            //判断用户名是否存在
            axios({
                method: "GET",
                url: URL + "/management/user/validateUserAccount",
                params: {
                    phoneNumber: this.state.accountCode,
                }
            }).then(res => {
                //用户不存在,向后台发送手机号和验证码
                if (res.data.code === 200) {
                    axios({
                        method: "POST",
                        url: URL + "/management/user/register",
                        params: {
                            phoneNumber: this.state.accountCode,
                            registerCode: this.state.captcha
                        }
                    }).then(res1 => {
                        //手机号和验证码匹配，注册成功
                        if (res1.data.code === 200) {
                            //设置cookie
                            setCookie(res1.data.nickName, res1.data.id,res1.data.images)
                            Toast.info("注册成功！")
                            //注册成功后跳转到风险测评界面
                            this.props.history.push("/riskEval")
                        } else {
                            Toast.fail("注册失败，请检查手机号和验证码是否正确")
                        }
                    })
                    //用户存在，提示用户此号码已注册，可直接登录
                }else {
                    Toast.info("此号码已注册，请直接登录")
                }
            })
        }

    }
    render() {
        return (
            <div>

                <NavBar
                    mode="light"
                    icon={<Icon type="left" style={{color:"#f8a83f"}}/>}
                    onLeftClick={() => this.props.history.push('/myNotLogin')}
                >登录/注册</NavBar>

                <div className="login-form">
                    <h2 className='login-title'>欢迎登录启明投顾系统</h2>
                    <div className="login-ac">
                        <List>
                            <InputItem
                                placeholder="请输入手机号"
                                onChange={this.get_accountCode}
                                value={this.state.accountCode}
                            />
                        </List>
                    </div>
                    <div className="login-captcha">

                            <InputItem
                                onChange={this.get_captcha}
                                value={this.state.captcha}
                                placeholder="请输入验证码"
                            />
                            &nbsp;&nbsp;&nbsp;
                            <Button style={{width:"30%", fontSize:"15px",height:"43px",backgroundColor:"#f8a83f",color:"#FFFFFF"}} className='btn btn-primary' onClick={this.valid}>获取验证码</Button>
                    </div>
                </div>
                <WhiteSpace/>
                <WhiteSpace/>
                <WhiteSpace/>
                <WhiteSpace/>
                <WhiteSpace/>
                <WhiteSpace/>
                <WhiteSpace/>
                <WhiteSpace/>
                <WhiteSpace/>
                <WhiteSpace/>
                <footer className='login-button-container'>
                    <Button style={{ float: "left", display: "block", width: "35%", margin: "0 10%", backgroundColor: "#f8a83f",color:"#FFFFFF" }}  onClick={this.login}>登录</Button><WhiteSpace />
                    <Button style={{ float: "left", display: "block", width: "35%", margin: "0 10%", backgroundColor: "#f8a83f" ,color:"#FFFFFF"}}  onClick={this.register}>注册</Button><WhiteSpace />
                </footer>
            </div>
        )
    }
}